﻿package net.wangit.view.chart.impl;

import javax.swing.ImageIcon;

import net.wangit.view.chart.Chart;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.DefaultDrawingSupplier;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.renderer.AbstractRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;


public abstract class AbstractChart extends Chart {

	public AbstractChart() {
	}

	protected JFreeChart createJFreeChart() {
		Plot plot = getPlot();
		configPlot(plot);
		JFreeChart chart = new JFreeChart(title, titleFont, plot, legend);
		configPane(chart);
		configTitle(chart);
		if (legend)
			configLegend(chart);
		return chart;
	}

	protected abstract Plot getPlot();

	private void configPane(JFreeChart chart) {
		chart.setAntiAlias(antiAlias);
		if (backgroundColor != null)
			chart.setBackgroundPaint(backgroundColor);
		if (backgroundImage != null) {
			ImageIcon temp = new ImageIcon(backgroundImage);
			chart.setBackgroundImage(temp.getImage());
			chart.setBackgroundImageAlignment(backgroundImageAlignment);
			chart.setBackgroundImageAlpha(backgroundImageAlpha);
		}
		chart.setBorderVisible(borderVisible);
		if (borderVisible) {
			if (borderColor != null)
				chart.setBorderPaint(borderColor);
			if (borderStroke != null)
				chart.setBorderStroke(borderStroke);
		}
		chart.setPadding(padding);
	}

	private void configPlot(Plot plot) {
		if (colors != null)
			plot.setDrawingSupplier(new DefaultDrawingSupplier(colors,
					DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE,
					DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
					DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
					DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE));
		if (plotBackgroundColor != null)
			plot.setBackgroundPaint(plotBackgroundColor);
		plot.setBackgroundAlpha(plotBackgroundAlpha);
		plot.setForegroundAlpha(plotForegroundAlpha);
		if (plotOutlineColor != null)
			plot.setOutlinePaint(plotOutlineColor);
		if (plotOutlineStroke != null)
			plot.setOutlineStroke(plotOutlineStroke);
		plot.setInsets(plotPadding);
	}

	private void configTitle(JFreeChart chart) {
		TextTitle title = chart.getTitle();
		title.setPosition(titlePosition);
		if (titleColor != null)
			title.setPaint(titleColor);
		if (titleBackgroundColor != null)
			title.setBackgroundPaint(titleBackgroundColor);
		if (titleBorder != null)
			title.setBorder(titleBorder);
		if (subTitle != null) {
			subTitle.setPosition(titlePosition);
			chart.addSubtitle(subTitle);
		}
	}

	private void configLegend(JFreeChart chart) {
		LegendTitle legend = chart.getLegend();
		legend.setPosition(legendPosition);
		if (legendFont != null)
			legend.setItemFont(legendFont);
		if (legendColor != null)
			legend.setItemPaint(legendColor);
		if (legendBackgroundColor != null)
			legend.setBackgroundPaint(legendBackgroundColor);
		if (legendBorder != null)
			legend.setBorder(legendBorder);
	}

	protected void configRenderer(AbstractRenderer renderer) {
		renderer.setItemLabelsVisible(itemLabelsVisible);
		renderer.setItemLabelAnchorOffset(itemLabelAnchorOffset);
		if (itemOutlineColor != null)
			renderer.setOutlinePaint(itemOutlineColor);
		if (itemOutlineStroke != null)
			renderer.setOutlineStroke(itemOutlineStroke);
	}
}
